package com.kgc.storeconsumer.controller;

import com.kgc.basic.util.ResponseMsg;
import com.kgc.store.pojo.ProMove;
import com.kgc.storeconsumer.feign.ProMoveFeign;
import com.kgc.storeconsumer.feign.ProMoveWorkFlowFeign;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@CrossOrigin
@RequestMapping("/proMove")
public class ProMoveController {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private ProMoveFeign proMoveFeign;
    @Autowired
    private ProMoveWorkFlowFeign proMoveWorkFlowFeign;

    @RequestMapping("GetOneProMove")
    @ResponseBody
    public ResponseMsg getOne(@RequestParam("code") String code){
        logger.info("ProMoveController getOne code:"+code);
        ResponseMsg responseMsg =proMoveFeign.getOne(code);
        logger.debug("proMoveFeign getOne responseMsg:"+responseMsg+",code:"+code);
        return responseMsg;
    }
    @RequestMapping("getProMovePage")
    @ResponseBody
    public ResponseMsg getPage(String pageNow, String pageSize, String code, String status, String startDate,String endDate){
        logger.info("ProMoveController getPage pageNow:"+pageNow+",pageSize:"+pageSize+",code:"+code+",status:"+status);
        Map<String,Object> map = new HashMap<>();
        map.put("code",code);
        map.put("status",status);
        map.put("startDate",startDate);
        map.put("endDate",endDate);
        map.put("pageNow",pageNow);
        map.put("pageSize",pageSize);
        ResponseMsg responseMsg =proMoveFeign.getPage(map);
        logger.debug("proMoveFeign getPage responseMsg:"+responseMsg+",map:"+map);
        return responseMsg;
    }
    @RequestMapping("addProMove")
    @ResponseBody
    public ResponseMsg add(@RequestBody ProMove proMove){
        logger.info("ProMoveController add proMove:"+proMove);
        Map<String,Object> variables = new HashMap<>();
        variables.put("code",proMove.getCode());//单据编号
        variables.put("type",proMove.getTypeId());//流程类型
        proMoveWorkFlowFeign.startProcess(variables);
        ResponseMsg responseMsg =proMoveFeign.add(proMove);
        logger.debug("proMoveFeign add responseMsg:"+responseMsg+",proMove:"+proMove);
        return responseMsg;
    }
    @RequestMapping("updateProMove")
    @ResponseBody
    public ResponseMsg update(@RequestBody ProMove proMove){
        logger.info("ProMoveController update proMove:"+proMove);
        ResponseMsg responseMsg =proMoveFeign.update(proMove);
        logger.debug("proMoveFeign update responseMsg:"+responseMsg+",proMove:"+proMove);
        return responseMsg;
    }
    @RequestMapping("delProMove")
    @ResponseBody
    public ResponseMsg del(@RequestParam("code") String code){
        logger.info("ProMoveController del code:"+code);
        ResponseMsg responseMsg =proMoveFeign.del(code);
        logger.debug("proMoveFeign del responseMsg:"+responseMsg+",code:"+code);
        return responseMsg;
    }
    @RequestMapping("changProMoveStatus")
    @ResponseBody
    public ResponseMsg changStatus(@RequestParam("code") String code,@RequestParam("status") String status){
        logger.info("ProMoveController changStatus code:"+code+",status:"+status);
        ResponseMsg responseMsg =proMoveFeign.changStatus(code,status);
        logger.debug("proMoveFeign changStatus responseMsg:"+responseMsg+",status:"+status);
        return responseMsg;
    }

    @RequestMapping("/check")
    @ResponseBody
    public  ResponseMsg check(@RequestBody Map<String,Object> map){
        ResponseMsg responseMsg = new ResponseMsg();
        logger.info("SalesOrdersController check params,{}",map.toString());
        Map<String,Object> variables = new HashMap<>();
        variables.put("code",(String) map.get("code"));//单据编号
        variables.put("checkManId",(Integer) map.get("checkManId"));//审批人
        variables.put("checkManName",(String) map.get("checkManName"));//审批人
        variables.put("checkComment",(String) map.get("checkComment"));//审批意见
        variables.put("checkDate",new Date());//审批时间
        variables.put("checkRes",(boolean)map.get("checkRes"));//审批结果
        com.kgc.activiti.util.ResponseMsg msg=  proMoveWorkFlowFeign.competeTask(variables);
        responseMsg.setCode(msg.getCode());
        responseMsg.setMsg(msg.getMsg());
        if(msg.getCode().equals("200")){

        }
        return responseMsg;
    }
}
